package cn.jiyun.controller;

import cn.jiyun.pojo.Users;
import cn.jiyun.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Fay
 * @date 2022/6/21&13:11
 */
@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 页面跳转
     */
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }

    /**
     * 添加用户
     */
    @RequestMapping("/addUser")
    public String userAdd(Users users){
        userService.insertUser(users);
        return "ok";
    }
    /**
     * 查询所有
     */
    @RequestMapping("/listUser")
    public String listUser(Model model){
        List<Users> usersList = userService.listUser();
        model.addAttribute("usersList",usersList);
        return "list";
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    public String login(Users users,Model model){
        Users user = userService.login(users);
        //如果user不等于空的话存到model里面返回给前端

            model.addAttribute("user",user);
            //重定向给users的list

        //返回给login页面
        return "ok";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String findDelete(Integer id){
        userService.findDelete(id);
        return "redirect:/users/listUser";
    }

    /**
     * 修改回显
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        Users users = userService.toUpdate(id);
        model.addAttribute("users",users);
        return "update";
    }

    /**
     * 修改
     */
    @RequestMapping("/updateUsers")
    public String updateUsers(Users users){
        userService.updateUsers(users);
        return "redirect:/users/listUser";
    }
}
